home *** CD-ROM | disk | FTP | other *** search
- package horst;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Shape;
- import java.awt.Toolkit;
-
- public class TextView extends View {
- boolean m_bSplittable = true;
- Color m_color;
- int m_maxTokenLength = -1;
- Font m_SubSuperScriptFont;
- char[] debug;
-
- public TextView(View parent, Element e, HTMLPane container) {
- super(parent, e, container);
- }
-
- protected boolean canSplit(int width) {
- if (!this.m_bSplittable) {
- return false;
- } else {
- char[] data = super.m_elem.getCharData();
- FontMetrics fm = ((View)this).getFontMetrics();
- int delim = -1;
- int len = data.length;
- if (len < 2) {
- return false;
- } else {
- int last = -1;
- int currLen = 0;
-
- int i;
- for(i = 0; i < len; ++i) {
- currLen += fm.charWidth(data[i]);
- if (currLen > width) {
- break;
- }
-
- if (Character.isWhitespace(data[i])) {
- last = i;
- }
- }
-
- if (i == len) {
- return false;
- } else {
- return last != -1;
- }
- }
- }
- }
-
- protected Color getColor() {
- return ((View)this).isLink() && super.m_elem.getHasFocus() ? super.m_elem.getFocusColor() : this.m_color;
- }
-
- protected int getDescent() {
- return ((View)this).getFontMetrics().getDescent();
- }
-
- protected int getMaximumTokenLength() {
- if (this.m_maxTokenLength > -1) {
- return this.m_maxTokenLength;
- } else {
- this.m_maxTokenLength = 0;
- FontMetrics fm = ((View)this).getFontMetrics();
- char[] data = super.m_elem.getCharData();
- int p1 = data.length;
-
- for(int i = p1 - 1; i >= 0; --i) {
- if (Character.isWhitespace(data[i])) {
- int offset = Math.min(i + 1, data.length);
- if (offset < data.length) {
- int nChars = p1 - i - 1;
- int strlen = fm.charsWidth(data, offset, nChars);
- this.m_maxTokenLength = Math.max(strlen, this.m_maxTokenLength);
- }
-
- p1 = i;
- }
- }
-
- if (p1 == data.length) {
- this.m_maxTokenLength = fm.charsWidth(data, 0, data.length);
- } else if (p1 > 0) {
- this.m_maxTokenLength = Math.max(this.m_maxTokenLength, fm.charsWidth(data, 0, p1));
- }
-
- return this.m_maxTokenLength;
- }
- }
-
- protected int getMinimumSpan(int axis) {
- return this.getPreferredSpan(axis);
- }
-
- protected int getPreferredSpan(int axis) {
- if (axis == 1 && super.m_prefWidth != -1) {
- return super.m_prefWidth;
- } else if (axis == 0 && super.m_prefHeight != -1) {
- return super.m_prefHeight;
- } else if (super.m_elem.m_p0 > super.m_elem.m_p1) {
- System.out.println("Element pointer corruption!!");
- super.m_prefWidth = 0;
- super.m_prefHeight = 0;
- return 0;
- } else {
- FontMetrics fm;
- if (axis != 1 || this.m_SubSuperScriptFont == null || !super.m_elem.isAttributeDefined("superscript") && !super.m_elem.isAttributeDefined("subscript")) {
- fm = ((View)this).getFontMetrics();
- } else {
- fm = Toolkit.getDefaultToolkit().getFontMetrics(this.m_SubSuperScriptFont);
- }
-
- switch (axis) {
- case 0:
- super.m_prefHeight = fm.getAscent();
- return super.m_prefHeight;
- case 1:
- char[] data = super.m_elem.getCharData();
- super.m_prefWidth = fm.charsWidth(data, 0, data.length);
- return super.m_prefWidth;
- default:
- return 0;
- }
- }
- }
-
- protected void init() {
- ((View)this).setInsets(0, 0, 0, 0);
- this.m_color = Utilities.setColorProperty(super.m_elem.getDocument().getTextColor(), "textcolor", super.m_elem.getAttributes());
- if (super.m_elem.isAttributeDefined("superscript") || super.m_elem.isAttributeDefined("subscript")) {
- this.m_SubSuperScriptFont = TextAttributes.createFont(((View)this).getFont(), "-2");
- }
-
- this.debug = super.m_elem.getCharData();
- }
-
- protected boolean isContainerView() {
- return false;
- }
-
- protected boolean isSplittable() {
- return true;
- }
-
- protected boolean isUnderlined() {
- boolean bLink = ((View)this).isLink();
- if (bLink && super.m_elem.m_relatedElements.size() == 0 && Utilities.isBlank(super.m_elem.getCharData())) {
- return false;
- } else if (bLink) {
- return super.m_container.m_props.m_bUnderLineLinks;
- } else {
- boolean bUnderLine = false;
- String src = (String)super.m_elem.getAttribute("underline");
- if (src != null) {
- bUnderLine = src.equals("true");
- }
-
- return bUnderLine;
- }
- }
-
- protected Rectangle layout(int x, int y, int width, LayoutInfo info) {
- width = this.getPreferredSpan(1);
- super.m_bounds = new Rectangle(x, y, width, this.getPreferredSpan(0));
- return super.m_bounds;
- }
-
- public void paint(Graphics g, Shape alloc) {
- Rectangle clip = alloc.getBounds();
- char[] data = super.m_elem.getCharData();
- if (super.m_bounds.intersects(clip) && data != null && data.length > 0) {
- Font oldFont = g.getFont();
- Color oldColor = g.getColor();
- Font f = ((View)this).getFont();
- g.setFont(f);
- FontMetrics fm = g.getFontMetrics();
- if (super.m_container.isTextSelected()) {
- int start = super.m_container.m_document.m_selectedP0;
- int end = super.m_container.m_document.m_selectedP1;
- if (start > end) {
- int save = start;
- start = end;
- end = save;
- }
-
- boolean bSelection = true;
- int xStart = 0;
- int xEnd = 0;
- if (super.m_elem.m_p0 <= start && super.m_elem.m_p1 >= start && super.m_elem.m_p1 <= end) {
- xStart = start;
- xEnd = super.m_elem.m_p1;
- } else if (super.m_elem.m_p0 >= start && super.m_elem.m_p1 <= end) {
- xStart = super.m_elem.m_p0;
- xEnd = super.m_elem.m_p1;
- } else if (super.m_elem.m_p0 >= start && super.m_elem.m_p0 <= end && super.m_elem.m_p1 >= end) {
- xStart = super.m_elem.m_p0;
- xEnd = end;
- } else if (super.m_elem.m_p0 <= start && super.m_elem.m_p1 >= end) {
- xStart = start;
- xEnd = end;
- } else {
- bSelection = false;
- }
-
- if (bSelection) {
- int len = xEnd - xStart + 1;
- char[] chardata = new char[len];
- StringBuffer buffer = ((View)this).getDocument().getTextBuffer();
- int buflen = buffer.length();
- int i = 0;
-
- for(int j = xStart; i < len && j < buflen; ++j) {
- chardata[i] = buffer.charAt(j);
- ++i;
- }
-
- int strWidth = fm.charsWidth(chardata, 0, chardata.length);
- int offset = 0;
- if (xStart > super.m_elem.m_p0) {
- int slen = xStart - super.m_elem.m_p0;
- char[] cdata = new char[slen];
- int i = 0;
-
- for(int j = super.m_elem.m_p0; i < slen; ++j) {
- cdata[i] = buffer.charAt(j);
- ++i;
- }
-
- offset = fm.stringWidth(new String(cdata));
- }
-
- g.setColor(Color.cyan);
- g.fillRect(super.m_bounds.x + offset, super.m_bounds.y, strWidth, super.m_bounds.height + this.getDescent());
- }
- }
-
- g.setColor(this.getColor());
- if (super.m_elem.isAttributeDefined("superscript") && this.m_SubSuperScriptFont != null) {
- g.setFont(this.m_SubSuperScriptFont);
- FontMetrics fmetrics = g.getFontMetrics();
- fmetrics = g.getFontMetrics();
- g.drawChars(data, 0, data.length, super.m_bounds.x, super.m_bounds.y + fmetrics.getAscent() - fmetrics.getLeading() - fm.getDescent());
- } else if (super.m_elem.isAttributeDefined("subscript") && this.m_SubSuperScriptFont != null) {
- g.setFont(this.m_SubSuperScriptFont);
- FontMetrics fmetrics = g.getFontMetrics();
- g.drawChars(data, 0, data.length, super.m_bounds.x, super.m_bounds.y + super.m_bounds.height + this.getDescent());
- } else {
- g.drawChars(data, 0, data.length, super.m_bounds.x, super.m_bounds.y + super.m_bounds.height);
- }
-
- if (this.isUnderlined()) {
- int y = super.m_bounds.y + super.m_bounds.height + this.getDescent() / 2;
- g.drawLine(super.m_bounds.x, y, super.m_bounds.x + fm.charsWidth(data, 0, data.length), y);
- }
-
- ((View)this).drawDebugBox(g, Color.black);
- g.setFont(oldFont);
- g.setColor(oldColor);
- }
-
- }
-
- protected void reset() {
- super.m_elem.reset();
- this.m_maxTokenLength = -1;
- }
-
- protected void setCanWrap(boolean bSplittable) {
- this.m_bSplittable = bSplittable;
- }
-
- protected boolean setToLineStarter() {
- if (super.m_elem.m_bInPreformat) {
- return true;
- } else {
- char[] data = super.m_elem.getCharData();
- boolean isBlank = true;
- int i = 0;
-
- int trimPos;
- for(trimPos = -1; i < data.length; trimPos = i) {
- if (!Character.isWhitespace(data[i++])) {
- isBlank = false;
- break;
- }
- }
-
- if (isBlank) {
- return false;
- } else {
- if (trimPos >= 0) {
- Element var10000 = super.m_elem;
- var10000.m_p0 += trimPos;
- }
-
- return super.m_elem.m_p0 <= super.m_elem.m_p1;
- }
- }
- }
-
- protected View[] split(int width) {
- char[] data = super.m_elem.getCharData();
- FontMetrics fm = ((View)this).getFontMetrics();
- int lastWhiteSpace = -1;
- int currentLen = 0;
- int len = data.length;
- int delim = -1;
- int lenminus1 = len - 1;
-
- for(int i = 0; i < len; ++i) {
- currentLen += fm.charWidth(data[i]);
- if (currentLen > width && delim > -1) {
- break;
- }
-
- if (Character.isWhitespace(data[i])) {
- delim = i;
- }
- }
-
- int lastPos = delim == -1 ? Math.max(0, len - 1) : delim;
- Element e1 = new Element(super.m_elem);
- e1.m_p0 = super.m_elem.m_p0;
- e1.m_p1 = e1.m_p0 + lastPos;
- e1.m_anchor = super.m_elem.m_anchor;
- boolean bIsLink = super.m_elem.isLink();
- if (bIsLink) {
- Element[] relatedElems = super.m_elem.getRelatedElements();
-
- for(int j = 0; j < relatedElems.length; ++j) {
- e1.addRelatedElement(relatedElems[j]);
- }
- }
-
- View v1 = super.m_container.m_viewFactory.createView((View)null, e1, super.m_container);
- if (e1.m_p1 != super.m_elem.m_p1 && delim != -1) {
- Element e2 = new Element(super.m_elem);
- e2.m_p0 = Math.min(super.m_elem.m_p1, e1.m_p1 + 1);
- e2.m_p1 = super.m_elem.m_p1;
- e2.m_anchor = super.m_elem.m_anchor;
- if (bIsLink) {
- Element[] relatedElems = super.m_elem.getRelatedElements();
-
- for(int j = 0; j < relatedElems.length; ++j) {
- e2.addRelatedElement(relatedElems[j]);
- }
- }
-
- View v2 = super.m_container.m_viewFactory.createView((View)null, e2, super.m_container);
- Element[] relates = super.m_elem.getRelatedElements();
-
- for(int j = 0; j < relates.length; ++j) {
- relates[j].addRelatedElement(e1);
- relates[j].addRelatedElement(e2);
- }
-
- e1.addRelatedElement(e2);
- e2.addRelatedElement(e1);
- if (super.m_elem.m_anchor != null) {
- Element[] newElems = new Element[]{e1, e2};
- super.m_elem.m_anchor.replaceRelatedElement(super.m_elem, newElems);
- }
-
- View[] vws = new View[2];
- vws[0] = v1;
- vws[1] = v2;
- return vws;
- } else {
- if (super.m_elem.m_anchor != null) {
- Element[] newElems = new Element[1];
- newElems[0] = e1;
- super.m_elem.m_anchor.replaceRelatedElement(super.m_elem, newElems);
- }
-
- View[] vws = new View[2];
- vws[0] = v1;
- vws[1] = null;
- return vws;
- }
- }
-
- protected ElementViewInfo viewToModel(int x, int y) {
- ElementViewInfo info = null;
- Rectangle bounds = ((View)this).getBounds();
- if (bounds.contains(x, y)) {
- FontMetrics fm = ((View)this).getFontMetrics();
- char[] pcdata = super.m_elem.getCharData();
- int strWidth = 0;
- if (pcdata.length > 0) {
- int nChars;
- int charWidth;
- for(nChars = 0; nChars < pcdata.length && bounds.x + strWidth <= x; strWidth += charWidth) {
- charWidth = fm.charWidth(pcdata[nChars++]);
- }
-
- new String(pcdata, 0, nChars);
- super.m_elem.m_textbufferPosition = super.m_elem.m_p0;
- if (nChars > 0) {
- Element var10000 = super.m_elem;
- var10000.m_textbufferPosition += nChars - 1;
- }
- }
-
- info = new ElementViewInfo(super.m_elem, this);
- }
-
- return info;
- }
- }
-